#include<avr/io.h>
#include"ACUAS.h"
#include"ACUASLCD-CSlab.h"


	//explanation of the resolution of the x and y axis
	//   5V - 1024 for ADC
	//2450mV - 501 - 90 degree
	//1650mV - 337 - 0  degree
	//850mV - 174 - -90 degree
	//so for 180 degree variation, the relevant mV variation is 2450-850=1600
	//the ratio consequently is 180 degree / 1600mV = 0,1125
	//0.1125*1.5=0.16875
#define RATIO 0.1125

VAR16(xcali=0)

VAR16(ycali=0)

	//initiate the board and LCD
	void initiation(){
	    DDRA  = 0b00000000; 
		PORTA = 0b11111111; 
		DDRC  = 0b11111111; 
		PORTC = 0b11111111;
		ACTIVATE_LCD
		ACTIVATE_ADC
		}

	//the functions which read the voltages for the x and y axes
	uint16_t xread(){
		uint16_t result=0;
		ADC_CHANNEL(1)
		ADCONVERT_MV(result)//START_ADC is included
		return result;
		}

	uint16_t yread(){
		uint16_t result=0;
		ADC_CHANNEL(2)
		ADCONVERT_MV(result)
		return result;
		}

	//read in the first pair of voltages for calibration
	void calibration(){
		LCD_TEXT("CALI DATA, PLZ")
	//	if(PINA&0b00001000){
			xcali=xread();
			ycali=yread();
	//		}
			CLEAR_LCD
			unsigned int i;
			for(i=0;i<50;i++){
			LCD_XY(1,1)
			LCD_TEXT("CALI DATA: ")
			LCD_XY(1,2)
			LCD_NUMBER(xcali)
			LCD_XY(7,2)
			LCD_NUMBER(ycali)
			}
		}

	//read in the following pairs of degree in difference
    uint16_t xdiff(){
		uint16_t xdiff=0;
		xdiff=(xread()-xcali)*RATIO;
		return xdiff;
		}

    uint16_t ydiff(){
		uint16_t ydiff=0;
		ydiff=(yread()-ycali)*RATIO;
		return ydiff;
		}

	//diplay the pair of degrees in x and y in the LCD.
	void show(){
		LCD_XY(1,2)
		LCD_NUMBER(xdiff())
		LCD_XY(7,2)
		LCD_NUMBER(ydiff())
		}


int main(){
	initiation();

	calibration();

	CLEAR_LCD

	while(1) show();	

	return 0;
}
